package com.partsoft.utils;

public abstract class NumericUtils {

	public static final char[] NUMBER_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	public static boolean isNumberValue(int value) {
		return (value >= 48) && (value <= 57);
	}

	public static boolean isNumeric(String value) {
		for (int i = 0; i < value.length(); i++) {
			if (!isNumberValue(value.charAt(i)))
				return false;
		}
		return true;
	}

	public static String trimNoneNumeric(String value) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < value.length(); i++) {
			char cValue = value.charAt(i);
			if (!isNumberValue(cValue))
				continue;
			buffer.append(cValue);
		}
		return buffer.toString();
	}

	public static int fromNumber(char value) {
		return (value >= '0') && (value <= '9') ? value - '0' : -1;
	}

	public static byte byteFromNumeric(String decimals) {
		byte value = 0;
		for (int i = 0; (decimals != null) && (i < decimals.length()); i++) {
			value = (byte) (value * 10);
			value = (byte) (value + (byte) (decimals.charAt(i) - '0'));
		}
		return value;
	}

	public static short shortFromNumeric(String decimals) {
		short value = 0;
		for (int i = 0; (decimals != null) && (i < decimals.length()); i++) {
			value = (short) (value * 10);
			value = (short) (value + (short) (decimals.charAt(i) - '0'));
		}
		return value;
	}

	public static int intFromNumeric(String decimals) {
		int value = 0;
		for (int i = 0; (decimals != null) && (i < decimals.length()); i++) {
			value *= 10;
			value += decimals.charAt(i) - '0';
		}
		return value;
	}

	public static long longFromNumeric(String decimals) {
		long value = 0L;
		for (int i = 0; (decimals != null) && (i < decimals.length()); i++) {
			value *= 10L;
			value += decimals.charAt(i) - '0';
		}
		return value;
	}

	public static int buildVersion(int major, int minjor) {
		return major * 1000 + minjor;
	}
}